<?php
/**
 * 
 *
 * @category   Application_Extensions
 * @package    Recorder
 * @copyright  Copyright 2006-2010 Idragonsolution Developments
 * @license    http://idragonsolution.com/
 * @version    $Id: Bootstrap.php 9747 2012-07-26 02:08:08Z Thien $
 * @author     Thien
 */
?>

<?php
  $this->headScript()
      ->appendFile($this->layout()->staticBaseUrl . 'application/modules/Recorder/externals/scripts/composer_recorder.js');
  $this->headScript()
      ->appendFile($this->layout()->staticBaseUrl . 'application/modules/Recorder/externals/scripts/swfobject.js');

  $allowed = 1;
  $user = Engine_Api::_()->user()->getViewer();
  $allowed_upload = (bool) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('recorder', $user, 'upload');
  if($allowed_upload) $allowed = 1;
?>



<script type="text/javascript">
  var recorder = null;
  en4.core.runonce.add(function() {
    recorder = new Composer.Plugin.Recorder({
      title : '<?php echo $this->translate('Add Recorder') ?>',
      lang : {
        'Add Recorder' : '<?php echo $this->string()->escapeJavascript($this->translate('Add Recorder')) ?>',
        'Select File' : '<?php echo $this->string()->escapeJavascript($this->translate('Select File')) ?>',
        'cancel' : '<?php echo $this->string()->escapeJavascript($this->translate('cancel')) ?>',
        'Attach' : '<?php echo $this->string()->escapeJavascript($this->translate('Attach')) ?>',
        'Loading...' : '<?php echo $this->string()->escapeJavascript($this->translate('Loading...')) ?>',
        'Choose Source': '<?php echo $this->string()->escapeJavascript($this->translate('Choose Source')) ?>',
      },
      allowed : <?php echo $allowed;?>,
      type : type,
      requestOptions : {
        'url' : en4.core.baseUrl + 'recorder/index/compose-upload/format/json/c_type/'+type,
        'subUrl': 'recorders/upload'
      }
    })
    
    var type = 'wall';
    if (composeInstance.options.type) type = composeInstance.options.type;
    composeInstance.addPlugin(recorder);
  });
  
  
   var recMessageArray = new Array()  
    recMessageArray[0] = "entering demo mode.";  
    recMessageArray[1] = "ready to go!";  
    recMessageArray[2] = "recording";  
    recMessageArray[3] = "stopped recording";  
    recMessageArray[4] = "stopped playing";  
    recMessageArray[5] = "ready!!";  
    recMessageArray[6] = "playing";  
    recMessageArray[7] = "starting to save";  
    recMessageArray[8] = "hmm. nothing to save";  
    recMessageArray[9] = "truncating the file to 10 seconds";  
    recMessageArray[10] = "encoding to MP3";  
    recMessageArray[11] = "...";  
    recMessageArray[12] = "encoding error";  
    recMessageArray[13] = "encode complete";  
    recMessageArray[14] = "uploading...";  
    recMessageArray[15] = "upload complete";  
    recMessageArray[16] = "record pause";  
    recMessageArray[17] = "play pause"; 
    
    
    var currentRecorderName = '';
    function makeNewFile(){
        var a = new Date();
        currentRecorderName = 'recorder_'+a.getTime()+'.mp3';
        return currentRecorderName;
    }
    function recorderMessage(x){
        if (x == 2 ) {
             var t = $('recorder_status_upload');
             if (t) t.destroy();
        }
        if (x == 15){
            var request_url = 'recorders/upload-info';
            var request = new Request.JSON({
                'url' : en4.core.baseUrl+request_url ,
                'onRequest' : function(){

                },
                'onSuccess' : function(responseJSON, responseText){
                    if (responseJSON.status == true){
                        var label = $('compose-recorder-menu');
                        var t = $('recorder_status_upload');
                        if (t) t.destroy();
                        
                        var el = new Element('span',{
                            'id' : 'recorder_status_upload',
                            'class': 'recorder_status_upload',
                            'text' : 'your file has been upload success.'
                        });
                        el.inject(label);
                        makeFormInput(responseJSON)
                    }
                }
                
            }).send();
        }
    } 
    
    function makeFormInput(params){
        var activity = $('activity-form');
        
        var t = $('recorder_input_data');
        if (t) t.destroy();
        
        var inputs = new Element('div',{
            'id' : 'recorder_input_data',
            'styles' : {
                'display':'none'
            }
        });
        
        var inputType = new Element('input',{
            'type' :'hidden',
            'name': 'attachment[type]',
            'value':'recorder'
        });
        var inputValue = new Element('input',{
            'type' : 'hidden',
            'name' : 'attachment[recorder_id]',
            'value': params.id
        });
        
        var title = $('compose-recorder-input-title');
        var inputTitle = new Element('input',{
            'type' : 'hidden',
            'name' : 'attachment[title]',
            'value': title.value
        });
        
        
        //inputType.inject(inputs);
       // inputValue.inject(inputs);
       // inputTitle.inject(inputs);
       // inputs.inject(activity);
        
        recorder.makeFormInputs({'title':title.value,'recorder_id':params.id})
    }
</script>
